package wjl.project.dubboelabauthconsumer;

import org.redisson.api.RedissonClient;
import org.redisson.misc.RedisURI;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.Session;
import org.springframework.web.bind.annotation.*;
import wjl.project.dubboelabauthapi.RedisKeys;
import wjl.project.dubboelabauthapi.ResultUtil;
import wjl.project.dubboelabauthapi.UserQo;
import wjl.project.dubboelabauthapi.UserService;
import wjl.project.dubboelabauthconsumer.Entity.EUser;
import wjl.project.dubboelabauthconsumer.SecurityConfig.SecurityUser;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * testController
 *
 * @blame Android Team
 */
@RestController
public class LoginController {

    @Resource
    FindByIndexNameSessionRepository<? extends Session> sessionRepository;

    @Resource
    UserService userService;

    @Resource
    RedissonClient redissonClient;

    @PostMapping("/auth/send_phone_code-v1")
    public ResultUtil.Result sendPhoneCode(@RequestParam String phone){
        Object key  = redissonClient.getBucket(RedisKeys.PHONE_KEY.getKey()+phone).get();
        if (key!=null){
            return ResultUtil.LOGIN_PHONE_CODE_SENT;
        }
        int flag = new Random().nextInt(999999);
        if (flag < 100000)
        {
            flag += 100000;
        }
        redissonClient.getBucket(RedisKeys.PHONE_KEY.getKey()+phone).set(flag,RedisKeys.PHONE_KEY.getTime(), TimeUnit.SECONDS);
        System.out.println(flag);
        return ResultUtil.LOGIN_PHONE_CODE_SEND;
    }


//
//
//
//
//
//    @PostMapping("/register")
//    public ResultUtil.Result registerUser(@RequestBody UserQo qo){
//        return userService.register(qo);
//    }
//
    @GetMapping(value = "/getinfo-v1",produces="application/json;charset=UTF-8")
    public ResultUtil.Result getUserInfo(){
        SecurityUser securityUser = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (securityUser!=null){
            System.out.println(securityUser.getUsername());
            return userService.getUserInfoByPhone(securityUser.geteUser().getMobilePhone());
        }else {
            return ResultUtil.SEARCH_USER_INVALID;
        }
    }
//
//
//
//
//    @PostMapping("/session")
//    public String getSessionId(HttpServletRequest httpServletRequest)
//    {
//        try {
//            SecurityUser securityUser =(SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//            System.out.println("当前登录的用户是 -->"+securityUser.getUsername());
//            System.out.println("当前会话id是 -->"+ httpServletRequest.getSession().getId());
//            Map<String,? extends Session> stringSessionMap =  sessionRepository.findByPrincipalName(securityUser.getUsername());
//            stringSessionMap.forEach((e,v)->{
//                System.out.println("该用户所使用的SessionId  --》"+v.getId());
//            });
//        }catch (Exception e){
//            e.printStackTrace();
//            return "账户未登录";
//        }
//        return "账户已登录";
//    }
//
//    @DeleteMapping("/session/{id}")
//    public String deleteSessionId(HttpServletRequest httpServletRequest, @PathVariable String id)
//    {
//        try {
//            SecurityUser securityUser =(SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//
//            System.out.println("当前会话id是 -->"+ httpServletRequest.getSession().getId());
//            System.out.println("删除的会话id是 -->"+id);
//            sessionRepository.deleteById(id);
//        }catch (Exception e){
//            e.printStackTrace();
//            return "账户未登录";
//        }
//        return "账户已登录";
//    }


}
